package com.newture.lxz.controller;

import com.baymax.docx.DocxData;
import com.baymax.docx.DocxUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.newture.lxz.controller.helper.Result;
import com.newture.lxz.db.mapper.*;
import com.newture.lxz.db.model.*;
import com.newture.lxz.generator.output.Column;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.nio.file.Paths;
import java.util.*;

@Controller
@RequestMapping("/form")
@Validated
public class FormController {

    @Resource
    LdTagsMapper ldTagsMapper;
    @Resource
    LdTagRelationshipMapper ldTagRelationshipMapper;
    @Resource
    LdGoodsMapper ldGoodsMapper;
    @Resource
    LdWidgetMapper ldWidgetMapper;
    @Resource
    LdCauseOfActionMapper ldCauseOfActionMapper;
    @Resource
    LdLawFirmMapper ldLawFirmMapper;
    @Resource
    LdCourtAddressMapper ldCourtAddressMapper;

    Logger logger = LogManager.getLogger(FormController.class);

    @Autowired
    private MessageSource messageSource;


    ObjectMapper jacksonMapper = new ObjectMapper();

    @GetMapping("/subCauses")
    @ResponseBody
    public Result subCauses(
            @RequestParam(name = "parent_id", required = false) String parent_id) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        Integer id = mapper.readValue(parent_id, Integer.class);

        LdCauseOfActionExample example = new LdCauseOfActionExample();
        LdCauseOfActionExample.Criteria cr = example.createCriteria();
        cr.andParentIdEqualTo(id);

        example.setDynamicColumns(new String[]{"cause_name","id","level","parent_id","path","priority","version"});
        List<LdCauseOfAction> list=ldCauseOfActionMapper.selectByExample(example);
        ArrayList<HashMap<String, Object>> data = new ArrayList<>();

        for(int j =0;j<list.size();j++){
            HashMap<String, Object> causeOfAction = new HashMap<>();
            causeOfAction.put("cause_name", list.get(j).getCauseName());
            causeOfAction.put("id", list.get(j).getId());
            causeOfAction.put("label", list.get(j).getCauseName());
            causeOfAction.put("leaf", isLeaf(list.get(j).getId()));
            causeOfAction.put("level", list.get(j).getLevel());
            causeOfAction.put("parent_id", list.get(j).getParentId());
            causeOfAction.put("path", list.get(j).getPath());
            causeOfAction.put("priority", list.get(j).getPriority());
            causeOfAction.put("value", list.get(j).getCauseName());
            causeOfAction.put("version", list.get(j).getVersion());
            data.add(causeOfAction);
        }
        return Result.success(data);
    }


    public Boolean isLeaf(int id){
        boolean flag = true;
        LdCauseOfActionExample example = new LdCauseOfActionExample();
        LdCauseOfActionExample.Criteria cr = example.createCriteria();
        cr.andParentIdEqualTo(id);

        example.setDynamicColumns(new String[]{"cause_name","id","level","parent_id","path","priority","version"});
        List<LdCauseOfAction> list=ldCauseOfActionMapper.selectByExample(example);
        //如果子节点为空，则返回true，说明位叶子节点
        flag = list.isEmpty();
        return flag;
    }

    @GetMapping("/lawFirm")
    @ResponseBody
    public Result lawFirm(
            @RequestParam(name = "name", required = false) String name) throws JsonProcessingException {

        LdLawFirmExample example = new LdLawFirmExample();
        LdLawFirmExample.Criteria cr = example.createCriteria();
        cr.andNameLike(name);

        example.setDynamicColumns(new String[]{"address","name"});
        List<LdLawFirm> list=ldLawFirmMapper.selectByExample(example);
        ArrayList<HashMap<String, Object>> data = new ArrayList<>();

        for(int j =0;j<list.size();j++){
            HashMap<String, Object> lawFirm = new HashMap<>();
            lawFirm.put("address", list.get(j).getAddress());
            lawFirm.put("name", list.get(j).getName());
            data.add(lawFirm);
        }
        return Result.success(data);
    }

    @GetMapping("/courts")
    @ResponseBody
    public Result courts(
            @RequestParam(name = "name", required = false) String name) throws JsonProcessingException {

        name = name + "%";
        LdCourtAddressExample example = new LdCourtAddressExample();
        LdCourtAddressExample.Criteria cr = example.createCriteria();
        cr.andCourtNameLike(name);

        example.setDynamicColumns(new String[]{"address","court_name","first_instance","id","province","remark","second_instance","version"});
        List<LdCourtAddress> list=ldCourtAddressMapper.selectByExample(example);
        ArrayList<HashMap<String, Object>> data = new ArrayList<>();

        for(int j =0;j<list.size();j++){
            HashMap<String, Object> courtAddress = new HashMap<>();
            courtAddress.put("address", list.get(j).getAddress());
            courtAddress.put("court_name", list.get(j).getCourtName());
            courtAddress.put("first_instance", list.get(j).getFirstInstance());
            courtAddress.put("id", list.get(j).getId());
            courtAddress.put("province", list.get(j).getProvince());
            courtAddress.put("remark", list.get(j).getRemark());
            courtAddress.put("second_instance", list.get(j).getSecondInstance());
            courtAddress.put("version", list.get(j).getVersion());
            courtAddress.put("_escaped__first_instance", escaped(list.get(j).getFirstInstance()));
            courtAddress.put("_escaped__second_instance", escaped(list.get(j).getSecondInstance()));
            data.add(courtAddress);
        }
        return Result.success(data);
    }

    public String escaped(String instance){
        if(instance.equals("y")) {
            return "是";
        }else{
            return "否";
        }
    }

}
